<?

//Data loading
$title='REPORT STATUS ADVANCE';
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. MASAJI PRAYASA CARGO';

$page   = 'PAGE';
$TableName = "ais_advance";
$DEFINE_FONT=0;
$pdf=new PDFReport("P","mm","Letter");
$pdf->AddPage();
$Result = ReportList ("", "", "");
if ($Result) $pdf->ReportTable($Result);
$pdf->Output();

function ReportList ($Space, $Index, $ParentID) {
		    global $RS,$Index,$TableName;
		    $DBConnection   = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
			if (!$Index) $Index = 0;
                        
                          $SQL = "
                                      SELECT $TableName.*, sum($TableName.jumlah) as sumjumlah, ais_memorial.nobkt as entry3, ais_memorial.jumlah as amount3, IF (L.nobkt<>'',L.nobkt,IF(LZ.nobkt<>'',LZ.nobkt,'')) as entry4, IF (L.jumlah<>'',L.jumlah,IF(LZ.jumlah<>'',LZ.jumlah,'')) as amount4, bb_bk.jumlah as amount2
                                      FROM `$TableName` 
                                      LEFT JOIN 
                                      ais_memorial bb_bk ON $TableName.nobkt=bb_bk.noref AND bb_bk.ktrasl=$TableName.ktrasl AND bb_bk.urut=999
                                      LEFT JOIN 
                                      ais_memorial ON $TableName.nobkt=ais_memorial.noref AND ais_memorial.ktrasl=$TableName.ktrasl AND ais_memorial.urut=999
                                      LEFT JOIN
                                      ais_memorial L ON L.noref=ais_memorial.nobkt AND L.ktrasl=$TableName.ktrasl AND L.urut=999
                                      LEFT JOIN
                                      ais_memorial_z LZ ON LZ.noref=ais_memorial.nobkt AND LZ.ktrasl=$TableName.ktrasl AND LZ.urut=999
                                    ";

                          $WHERE   = "$TableName.ktrasl='".GetOfficeID()."' ";
                          $WHERE  .=  " AND ais_memorial.noref<>''";
                          $WHERE  .=  " AND ifnull(bb_bk.jumlah,0)-ifnull(ais_memorial.jumlah,0) <> 0 AND IF (L.nobkt<>'',L.nobkt,IF(LZ.nobkt<>'',LZ.nobkt,''))='' ";

                          if (strlen($WHERE)) {
                                  $SQL = $SQL."WHERE ".$WHERE;
                          }
                        
                          $SQL .= " GROUP BY $TableName.nobkt";
  
                          $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
                          return $MyResult;
} 

class PDFReport extends PDF {

	function Header() {
		global $title,$perush,$page,$DEFINE_FONT;
		if ($DEFINE_FONT==0) {
			$this->AddFont('arialn','','arialn.php');
			$DEFINE_FONT=1;
		}
		$this->SetFont('arialn','',12);
                $this->Cell(195,5,$perush,0,1,'C',0);
                $this->Cell(195,5,$title,0,1,'C',0);
                $SystemDate = date("d/m/Y");
                $this->Cell(195,5,"REPORT DATE: ".$SystemDate,0,1,'C',0);
                $this->Cell(195,5,$page." ".$this->PageNo(),0,1,'R',0);
                $this->Line(10,30,205,30);
                $this->Ln();
                $this->Ln();
                //Header

                //Column widths
                $w=array(10,27,45,20,27,20,27,20,27,20);
                $this->SetFont('arialn','',10);
                $this->Cell($w[0],7,"NO",1,0,'C',0);
                $this->Cell($w[1],7,"TRANS NO",1,0,'C',0);
                $this->Cell($w[2],7,"PAY TO",1,0,'C',0);
                $this->Cell($w[3],7,"AMOUNT",1,0,'C',0);
                $this->Cell($w[4],7,"BK/BB",1,0,'C',0);
                $this->Cell($w[5],7,"AMOUNT",1,0,'C',0);
                $this->Cell($w[6],7,"MEMORIAL",1,0,'C',0);
                $this->Cell($w[7],7,"AMOUNT",1,0,'C',0);
                $this->Ln();    	
	}

	function ReportTable($Result) {
	    //Column widths
	    $w=array(10,27,45,20,27,20,27,20,27,20);
	    //Data
	    foreach($Result as $row)
	    {
                $no++;
	        $this->Cell($w[0],6,$no,'LR');
	        $this->Cell($w[1],6,$row["nobkt"],'LR');
                if (strlen($row["debnam"])>20) $row["debnam"] = substr($row["debnam"], 0, 20); 
	        $this->Cell($w[2],6,$row["debnam"],'LR');
	        $this->Cell($w[3],6,number_format($row["sumjumlah"],2),'LR',0,'R');
	        $this->Cell($w[4],6,$row["aslref"],'LR',0,'R');
                $this->Cell($w[5],6,number_format($row["amount2"],2),'LR',0,'R');
                $this->Cell($w[6],6,$row["entry3"],'LR',0,'R');
                $this->Cell($w[7],6,number_format($row["amount3"],2),'LR',0,'R');
	        $this->Ln();
	    }
	    //Closure line
	    //$this->Cell(array_sum($w),0,'','T');
	}

	function Footer() {
	    //Closure line
	    $this->Cell(196,0,'','T');
	}

}


?>
